<template>
  <el-dialog
    :title="!dataForm.deptId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父部门id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父部门id"></el-input>
    </el-form-item>
    <el-form-item label="祖级列表" prop="ancestors">
      <el-input v-model="dataForm.ancestors" placeholder="祖级列表"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="deptName">
      <el-input v-model="dataForm.deptName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="leader">
      <el-input v-model="dataForm.leader" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="部门状态（0正常 1停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="部门状态（0正常 1停用）"></el-input>
    </el-form-item>
    <el-form-item label="删除标志（0代表存在 2代表删除）" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="删除标志（0代表存在 2代表删除）"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          deptId: 0,
          parentId: '',
          ancestors: '',
          deptName: '',
          orderNum: '',
          leader: '',
          phone: '',
          email: '',
          status: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          remark: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '父部门id不能为空', trigger: 'blur' }
          ],
          ancestors: [
            { required: true, message: '祖级列表不能为空', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '显示顺序不能为空', trigger: 'blur' }
          ],
          leader: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '部门状态（0正常 1停用）不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标志（0代表存在 2代表删除）不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.deptId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.deptId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysdept/info/${this.dataForm.deptId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.sysDept.parentId
                this.dataForm.ancestors = data.sysDept.ancestors
                this.dataForm.deptName = data.sysDept.deptName
                this.dataForm.orderNum = data.sysDept.orderNum
                this.dataForm.leader = data.sysDept.leader
                this.dataForm.phone = data.sysDept.phone
                this.dataForm.email = data.sysDept.email
                this.dataForm.status = data.sysDept.status
                this.dataForm.delFlag = data.sysDept.delFlag
                this.dataForm.createBy = data.sysDept.createBy
                this.dataForm.createTime = data.sysDept.createTime
                this.dataForm.updateBy = data.sysDept.updateBy
                this.dataForm.updateTime = data.sysDept.updateTime
                this.dataForm.remark = data.sysDept.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysdept/${!this.dataForm.deptId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'deptId': this.dataForm.deptId || undefined,
                'parentId': this.dataForm.parentId,
                'ancestors': this.dataForm.ancestors,
                'deptName': this.dataForm.deptName,
                'orderNum': this.dataForm.orderNum,
                'leader': this.dataForm.leader,
                'phone': this.dataForm.phone,
                'email': this.dataForm.email,
                'status': this.dataForm.status,
                'delFlag': this.dataForm.delFlag,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
